
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
    <title>Title</title>
    <link rel="stylesheet" href="<%=basePath%>/css/zTreeStyle/zTreeStyle.css">
    <script src="<%=basePath%>/js/jquery-2.1.0.js"></script>
    <script src="<%=basePath%>/js/jquery.ztree.all-3.5.js"></script>
    <script>
        var setting = {
            data: {
                simpleData: {
                    enable: true,
                    idKey: "id",
                    pIdKey: "pId",
                    rootPId: 0

                },
                key: {
                    name:"name"

                }
            },
            view: {
                dblClickExpand: false
            },
            callback: {
                onClick: zTreeOnClick,
                onRightClick: zTreeOnRightClick
            }
        };
        function zTreeOnClick(event,treeId, treeNode) {
           alert(treeNode.id+treeNode.name);
        }
        function zTreeOnRightClick(event, treeId, treeNode) {
            alert(treeNode+treeId);
            var zTree, rMenu;
            zTree = $.fn.zTree.getZTreeObj("tree");
            rMenu = $("#rMenu");
        }
        var treeNodes = [
            {"id":1, "pId":0, "name":"test1"},
            {"id":111, "pId":11, "name":"test111"},
            {"id":12, "pId":1, "name":"test12"},
            {"id":11, "pId":1, "name":"test11"}

        ];
        $(function() {
            //console.log(treeNodes)
            zTreeObj = $.fn.zTree.init($("#tree"), setting, treeNodes);
        });
        function addNode(id,pid) {
            var treeNode =  {"id":id, "pId":pid, "name":"test1"};
            var zTree = $.fn.zTree.getZTreeObj("tree");
            var parentZNode = zTree.getNodeByParam("id", pid, null); //获取父节点
            zTree.addNodes(parentZNode,treeNode);
        }

    </script>
</head>
<body>
<ul id="tree" class="ztree"></ul>
<button onclick="addNode(3,0)">1</button>
<button onclick="addNode(4,3)">2</button>
</body>
</html>
